from toee import *
from utilities import *
from py00439script_daemon import record_time_stamp, get_v, set_v, tsc, within_rect_by_corners



def san_dialog( attachee, triggerer ):
	if (not attachee.has_met(triggerer)):
		record_time_stamp(501)
		if (game.global_flags[91] == 1):
			triggerer.begin_dialog( attachee, 100 )
		else:
			triggerer.begin_dialog( attachee, 1 )
	else:
		triggerer.begin_dialog( attachee, 300 )
	return RUN_DEFAULT

def san_dying( attachee, triggerer ):
	game.global_flags[104] = 1
	record_time_stamp(456)
	return RUN_DEFAULT

def san_resurrect( attachee, triggerer ):
	game.global_flags[104] = 0
	return RUN_DEFAULT

def san_first_heartbeat( attachee, triggerer ):
	if (attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
		game.global_vars[727] = 0
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	if (not game.combat_is_active()):
		for obj in game.obj_list_vicinity(attachee.location,OLC_PC):
			if (not attachee.has_met(obj)):
				if (is_safe_to_talk(attachee,obj)):
					record_time_stamp(501)
					if (game.global_flags[91] == 1):
						obj.turn_towards(attachee)	## added by Livonya
						attachee.turn_towards(obj)	## added by Livonya
						obj.begin_dialog( attachee, 100 )
					elif ( (game.global_flags[107] == 1) or (game.global_flags[105] == 1) ):
						obj.turn_towards(attachee)	## added by Livonya
						attachee.turn_towards(obj)	## added by Livonya
						obj.begin_dialog( attachee, 590 )
					else:
						obj.turn_towards(attachee)	## added by Livonya
						attachee.turn_towards(obj)	## added by Livonya
						obj.begin_dialog(attachee,1)
						##game.new_sid = 0		## removed by Livonya
	if (game.global_vars[727] == 0 and attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
		attachee.cast_spell(spell_owls_wisdom, attachee)
		attachee.spells_pending_to_memorized()
	if (game.global_vars[727] == 4 and attachee.leader_get() == OBJ_HANDLE_NULL and not game.combat_is_active()):
		attachee.cast_spell(spell_shield_of_faith, attachee)
		attachee.spells_pending_to_memorized()
	game.global_vars[727] = game.global_vars[727] + 1
	return RUN_DEFAULT

def san_enter_combat( attachee, triggerer ):
	game.global_flags[347] = 0
	return RUN_DEFAULT

def san_start_combat( attachee, triggerer ):
	if ( obj_percent_hp(attachee) < 50 and (not attachee.has_met(triggerer)) ):
		found_pc = OBJ_HANDLE_NULL
		for pc in game.party:
			if pc.type == obj_t_pc:
				found_pc = pc
				attachee.ai_shitlist_remove( pc )
		if found_pc != OBJ_HANDLE_NULL:
			record_time_stamp(501)
			found_pc.begin_dialog( attachee, 200 )
			return SKIP_DEFAULT
	return RUN_DEFAULT

def talk_Hedrack( attachee, triggerer, line):
	npc = find_npc_near(attachee,8046)
	if (npc != OBJ_HANDLE_NULL):
		triggerer.begin_dialog(npc,line)
		npc.turn_towards(attachee)
		attachee.turn_towards(npc)
	else:
		triggerer.begin_dialog(attachee,580)
	return SKIP_DEFAULT

def escort_below( attachee, triggerer ):
	# game.global_flags[144] = 1
	game.global_vars[691] = 1
	attachee.standpoint_set( STANDPOINT_DAY, 267 )
	attachee.standpoint_set( STANDPOINT_NIGHT, 267 )
	game.fade_and_teleport(0,0,0,5080,478,451)
	return RUN_DEFAULT